import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { useMessage } from '@/hooks/web/useMessage';
import dayjs from 'dayjs';
import { Tinymce } from '@/components/Tinymce';
// http://boot3.jeecg.com/system/notice
type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '标题',
    dataIndex: 'roleName',
    align: 'left',
    fixed: 'left'
  },
  {
    title: '消息类型',
    dataIndex: 'roleValue',
    width: 180,
  },
  {
    title: '发布人',
    dataIndex: 'orderNo',
    width: 150,
  },
  {
    title: '优先级',
    dataIndex: 'orderNo',
    width: 150,
  },
  {
    title: '通告对象',
    dataIndex: 'orderNo',
    width: 150,
  },
  {
    title: '发布状态',
    dataIndex: 'status',
    width: 120,
    sorter: true,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === '1',
        checkedChildren: '停用',
        unCheckedChildren: '启用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? '1' : '0';
          const { createMessage } = useMessage();
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '发布时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    field: '[startDate, endDate]',
    label: '日期范围',
    labelWidth: 80,
    component: 'RangePicker',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      placeholder: ['开始时间', '结束时间'],
      showTime: { format: 'HH:mm:ss' },
    },
    defaultValue:[dayjs(new Date(new Date().getFullYear() - 1, new Date().getMonth(), new Date().getDate())).format('YYYY-MM-DD'),dayjs(new Date()).format('YYYY-MM-DD')]
  },
  {
    field: 'type',
    label: '消息类型',
    labelWidth: 80,
    component: 'Select',
    componentProps: {
      options: [
        { label: '系统消息', value: '1' },
        { label: '通知公告', value: '2' },
      ],
    },
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'field7',
    component: 'RadioGroup',
    label: '消息类型',
    defaultValue:'2',
    componentProps: {
      options: [
        {
          label: '系统消息',
          value: '1',
        },
        {
          label: '通知公告',
          value: '2',
        },
      ],
    },
    rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
  },
  {
    field: 'roleName',
    label: '标题',
    required: true,
    component: 'Input',
  },
  {
    field: 'roleValue',
    label: '摘要',
    required: true,
    component: 'InputTextArea',
  },
  {
    field: 'touser',
    component: 'RadioGroup',
    label: '接收用户',
    defaultValue:'1',
    componentProps: {
      options: [
        {
          label: '全体用户',
          value: '1',
        },
        {
          label: '指定用户',
          value: '2',
        },
      ],
    },
    rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
  },
  {
    field: 'toUserIds',
    component: 'Input',
    label: '指定用户',
    required: true,
    show: ({ values }) => {
      return values.touser == '2';
    },
    slot: 'customSlot',
  },
  {
    field: 'level',
    component: 'RadioGroup',
    label: '优先级',
    defaultValue:'2',
    componentProps: {
      options: [
        {
          label: '高',
          value: '1',
        },
        {
          label: '中',
          value: '2',
        },
        {
          label: '低',
          value: '3',
        },
      ],
    },
    rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
  },
  {
    field: 'status',
    component: 'RadioGroup',
    label: '发布状态',
    defaultValue: '1',
    componentProps: {
      options: [
        {
          label: '发布',
          value: '1',
        },
        {
          label: '待发布',
          value: '0',
        },
      ],
    },
    rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
  },
  {
    field: 'description',
    component: 'Input',
    label: '正文',
    defaultValue: 'defaultValue',
    rules: [{ required: true }],
    render: ({ model, field }) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      });
    },
  },
];
